//
//  VVWelcomeViewController.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/30.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class VVWelcomeViewController: UIViewController {

    //背景
    fileprivate lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    fileprivate lazy var headImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))

        //判断头像是否为nil,不为,nil加载远程图片地址
        if let imageUrl = VVUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named:"avatar_default_big"))
            
        }
        
        //设置圆角
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    //消息
    fileprivate lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        
        //判断用户名是否存在
        if let name = VVUserAccountViewModel.sharedViewModel.userAccount?.name {
            
            label.text = "欢迎回来,\(name)"
        } else {
            
            label.text = "欢迎回来"
        }
        
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        
        return label
        
    }()
    
    //重写 自定义视图
    override func loadView() {
        
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    //添加约束
   fileprivate func setupUI() {
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
    
    headImageView.snp.makeConstraints { (make) in
        
        make.centerX.equalTo(view)
        make.size.equalTo(CGSize(width: 90, height: 90))
        make.top.equalTo(view).offset(200)
    }
    
    messageLabel.snp.makeConstraints { (make) in
        
        make.top.equalTo(headImageView.snp.bottom).offset(10)
        make.centerX.equalTo(headImageView)
    }
        
    }
    
    //页面已经显示
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //执行弹簧动画
        startAnimation()
        
    }
    
    //开启弹簧动画
    fileprivate func startAnimation() {
        
        //修改透明度
        messageLabel.alpha = 0
        
        //更新约束
        headImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
            
        }
        
        //执行动画
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            
            //执行约束动画
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            
            UIView.animate(withDuration: 1, animations: { 
                
                //显示Label
                self.messageLabel.alpha = 1
                
            }, completion: { (_) in
                
                print("切换到根视图控制器")
                
                NotificationCenter.default.post(name:NSNotification.Name(SwitchRootVCNotification),object: nil)
            })
        }
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
